package com.xiaodou.mjf.application.core.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import com.xiaodou.mjf.application.core.repository.InvestCrowdControlRepository;
import com.xiaodou.mjf.application.core.service.InvestCrowdControlService;
import com.xiaodou.mjf.entity.core.InvestCrowdControl;
import com.xiaodou.mjf.infrastructure.common.app.JpaSimpleService;

/**
 * InvestCrowdControl业务访问实现
 *
 * @author XiaoDou.com Code Generator
 * @version 1.0 2014-10-29 17:37:52
 */
@Service
@Transactional
public class InvestCrowdControlServiceImpl extends JpaSimpleService<InvestCrowdControl, String> implements InvestCrowdControlService {

	Logger logger = LoggerFactory.getLogger(InvestCrowdControlServiceImpl.class);

	@Resource
	InvestCrowdControlRepository investCrowdControlRepository;

	@Override
	protected JpaRepository<InvestCrowdControl, String> getRepository() {
		return investCrowdControlRepository;
	}

	@Override
	public List<InvestCrowdControl> findByLimitId(String limitId) {
		return investCrowdControlRepository.findByLimitId(limitId);
	}

	@Override
	public Page<InvestCrowdControl> controlpage(InvestCrowdControl entity) {
		return investCrowdControlRepository.findAll((root, query, cb) -> {
			Predicate p1 = cb.equal(root.get("limitId").as(String.class), entity.getLimitId());
			Predicate p2 = cb.like(root.get("crowdName").as(String.class), "%" + entity.getCrowdName() + "%");
			return query.where(p1, p2).getRestriction();
		}, preparePage(entity, new Sort(new Sort.Order(Sort.Direction.ASC, "level"))));
	}

	@Override
	public Integer findByMaxLevel(String id) {
		return investCrowdControlRepository.findByMaxLevel(id);
	}

	@Override
	public InvestCrowdControl findByLevelAndLimitId(Integer level,
			String limitId) {
		return investCrowdControlRepository.findByLevelAndLimitId(level, limitId);
	}

	@Override
	public Integer findMaxLevelByLimitId(String limitId) {
		return investCrowdControlRepository.findMaxLevelByLimitId(limitId);
	}


}
